from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QPropertyAnimation
from com.atzl.interaction.WindowMessage import MessageWindow
from com.atzl.interaction.WindowPriceTable import PriceTableWindow
from com.atzl.interaction.WindowNwPriceTable import NWPriceTableWindow
from com.atzl.common.Win import Windows
from com.atzl.common.Datas import MessagesData
from com.atzl.common.StatusCode import StatusCode
from com.atzl.common.ActionStates import ActionStates
from com.atzl.ui.PassWordUI import Ui_Form as ps
from com.atzl.functions.accounting.func.Logs import CheckLogs
from com.atzl.functions.accounting.func.SetPrice import SetPrice


# 确认密码窗口交互
class PassWordWindow(QMainWindow, CheckLogs):  # 继承:QMainWindow, CheckLogs

    def __init__(self):
        QMainWindow.__init__(self)
        # 设置为全局小部件
        self.ui = ps()
        self.set = SetPrice()
        self.setprice = PriceTableWindow()
        self.nwsetprice = NWPriceTableWindow()
        self.message = MessageWindow()
        self.ui.setupUi(self)
        global widgets
        widgets = self.ui
        global set
        set = self.set

        widgets.lineEdit.setEchoMode(QLineEdit.Password)
        widgets.pushButton_6.pressed.connect(lambda:
                                             widgets.lineEdit.setEchoMode(QLineEdit.Normal))
        widgets.pushButton_6.released.connect(lambda:
                                              widgets.lineEdit.setEchoMode(QLineEdit.Password))
        widgets.lineEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.contextMenu = QMenu(widgets.lineEdit)
        self.affirm = self.contextMenu.addAction('确认')
        self.escape = self.contextMenu.addAction('退出')
        self.claerx = self.contextMenu.addAction('退出')
        self.affirm.triggered.connect(self.affirm_the_password)
        self.escape.triggered.connect(self.close)
        self.claerx.triggered.connect(widgets.lineEdit.clear)

    def get_text_password(self):
        a = widgets.lineEdit.text()
        return a

    def affirm_the_password(self):
        z = self.get_text_password()
        if z == '' or z is None:
            self.iftext_null()
            return
        a = set.affirm_password(self.get_text_password())
        if a == 1:
            datax = set.get_field_message(MessagesData.ANALYSIS_PATH_RICE)
            if str(datax) == str(StatusCode.TABLE_ISHAVE):
                self.ifhave_table()
            elif str(datax) == str(StatusCode.OPERATION_LOSE):
                self.to_message(StatusCode.OPERATION_LOSE)
                self.logs_in(StatusCode.OPERATION_LOSE)
            elif MessagesData.NW_TEXT != '无':
                nw_data = set.get_nw_price(MessagesData.OBJ_ID_PRICE)
                self.to_pricetable(datax)
                self.to_nw_pricetable(nw_data)
            else:
                self.to_pricetable(datax)
        elif a == -1:
            widgets.lineEdit.clear()
            self.not_pass()
        else:
            widgets.lineEdit.clear()
            self.no_pass()



    def to_pricetable(self, data):
        MessagesData.SET_PRICE = data
        Windows.PRICETABLE_WIN = self.setprice
        Windows.PRICETABLE_WIN.set_table_data()
        Windows.PRICETABLE_WIN.show()
        widgets.lineEdit.clear()
        Windows.SETOFFER_WIN.show()
        self.close()
        self.logs_in(ActionStates.RIGHTPASSWORDCUTOFFERTABLE)

    def to_nw_pricetable(self, data):
        MessagesData.SET_NW_PRICE = data
        Windows.NWPRICETABLE_WIN = self.nwsetprice
        Windows.NWPRICETABLE_WIN.set_table_data()
        Windows.NWPRICETABLE_WIN.show()
        self.logs_in(ActionStates.RIGHTPASSWORDCUTNWOFFERTABLE)

    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        if event.key() == QtCore.Qt.Key_Enter or event.key() == QtCore.Qt.Key_Return:
            self.affirm_the_password()
        if event.key() == QtCore.Qt.Key_Escape:
            widgets.lineEdit.clear()
            self.close()
            self.logs_in(ActionStates.GIVEUPINPUTPASSWORD)
            Windows.SETOFFER_WIN.show()
        if event.key() == QtCore.Qt.Key_Delete:
            widgets.lineEdit.clear()


    # 如果密码框为空,提示
    def iftext_null(self):
        self.to_message("密码不能为空!")


    # 如果有报价表界面提示并返回原来界面和回传用户行为监控数据至数据库
    def ifhave_table(self):
        widgets.lineEdit.clear()  # 清空密码输入框
        self.close()  # 关闭确认密码页面
        Windows.SETOFFER_WIN.show()  # 显示 设置报价页面
        self.to_message("该客户报价已创建!")  # 消息提示
        self.logs_in(ActionStates.HINTREPETITIONSETOFFER)  # 回传用户行为


    # 因为密码有误未能跳转报价预览,做出的界面提示和回传用户行为监控数据至数据库
    def not_pass(self):
        self.to_message("密码错误!")  # 消息提示
        self.logs_in(ActionStates.CONFIRMPASSWORDERROR)  # 回传用户行为


    # 因为异常未能跳转报价预览,做出的界面提示和回传用户行为监控数据至数据库
    def no_pass(self):
        self.to_message("请重新登录!")  # 消息提示
        self.logs_in(ActionStates.SUGGESTANEWLOGIN)  # 回传用户行为


    # 当确认密码页面显示时的动画
    def show_anim(self):
        self.anim0 = QPropertyAnimation(self, b"windowOpacity")  # 设置动画对象
        self.anim0.setDuration(350)  # 设置动画时长
        self.anim0.setStartValue(0)  # 设置初始属性,0为完全透明
        self.anim0.setEndValue(1)  # 设置结束属性,1.0为不透明
        self.anim0.start()  # 开始动画


    # 弹出消息提示框
    #   参数:
    #       text:提示内容
    def to_message(self, text):
        Windows.MESSAGE_WIN.set_message(text)
